package easy;

public class LongestCommonPrefix {
    //note:数组为空
    //后续字符串长度小于前字符串
    public static String longestCommonPrefix(String[] strs) {
        String prefix = "";
        if(strs.length!=0) {
            int shortestLength = 9999;
            int position = 0;
            for(int i=0;i<strs.length;i++){
                if(strs[i].length()<shortestLength){
                    shortestLength = strs[i].length();
                    position = i;
                }
            }
            String index = strs[position];
            if(index.length()>0 && strs.length>1) {
                for (int i = 0; i < index.length(); i++) {
                    boolean flag = true;
                    for (int j = 0; j < strs.length; j++) {
//                        System.out.println(index.charAt(i));
//                        System.out.println(strs[j].charAt(i));
//                        System.out.println(j);
//                        System.out.println(flag);
                        if (index.charAt(i) != strs[j].charAt(i)) {
                            flag = false;
                            break;
                        }

                    }
                    if (flag) {
                        prefix += index.charAt(i);
                    }else{
                        break;
                    }
                }
            }else{
                return index;
            }
        }
        return prefix;
    }
        public static  void  main(String[]  args)  {
        String []strs ={"aca","cba"};
            System.out.println("prefix:"+longestCommonPrefix(strs));
    }
}
